package com.llt.chatroom.controller;

import com.llt.chatroom.model.MessageSessionUser;
import com.llt.chatroom.service.MessageService;
import com.llt.chatroom.service.SessionService;
import com.llt.chatroom.model.Friend;
import com.llt.chatroom.model.MessageSession;
import com.llt.chatroom.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Slf4j
@RestController
public class SessionController {

    @Resource
    SessionService sessionService;

    @Resource
    MessageService messageService;

//    获取会话列表
    @GetMapping("/sessionList")
    public Object getMessageSessionList(HttpServletRequest request) {

        List<MessageSession> messageSessionList = new ArrayList<>();

        // 1. 通过会话拿到当前的用户id
        HttpSession session = request.getSession(false);
        if (session == null) {
            log.error("session为空");
            return messageSessionList;
        }
        User user = (User) session.getAttribute("user");
        if (user == null) {
            log.error("user为空");
            return messageSessionList;
        }

        // 2. 根据userId查询该用户在哪个会话中
        List<Integer> sessionIdList = sessionService.getSessionIdByUserId(user.getUserId());

        // 3. 遍历sessionId，查询每个会话里有哪些好友和最后一条信息
        for (int sessionId: sessionIdList) {

            MessageSession messageSession = new MessageSession();
            messageSession.setSessionId(sessionId);

            List<Friend> friends = sessionService.getFriendsBySessionId(sessionId, user.getUserId());
            messageSession.setFriends(friends);

            String lastMessage = messageService.getLastMessageBySessionId(sessionId);
            if (lastMessage == null) {
                messageSession.setLastMessage("");
            }else {
                messageSession.setLastMessage(lastMessage);
            }

            messageSessionList.add(messageSession);
        }

        log.info("获取消息列表成功");
        return messageSessionList;
    }


//    新增会话
    @Transactional
    @PostMapping("/session")
    public Object addMessageSession(int toUserId, @SessionAttribute("user") User user) {

        HashMap<String, Integer> resp = new HashMap<>();

        // 进行数据库插入(会话表)
        MessageSession messageSession = new MessageSession();
        sessionService.addMessageSession(messageSession); // 插入之后通过数据库的自增主键给 sessionId赋值了

        // 插入用户到会话里的 " 群 "
        MessageSessionUser messageSessionUserSelf = new MessageSessionUser();
        messageSessionUserSelf.setSessionId(messageSession.getSessionId());
        messageSessionUserSelf.setUserId(user.getUserId());
        sessionService.addMessageSessionUser(messageSessionUserSelf);

        MessageSessionUser messageSessionUserFriend = new MessageSessionUser();
        messageSessionUserFriend.setSessionId(messageSession.getSessionId());
        messageSessionUserFriend.setUserId(toUserId);
        sessionService.addMessageSessionUser(messageSessionUserFriend);

        log.info("添加新用户会话成功: " + "(" + "用户id: " + toUserId + " )");

        return resp.put("sessionId", messageSession.getSessionId());
    }


}
